package com.polaris.live.common.util.loading

import android.content.Context
import androidx.annotation.StringRes
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.common.util.ViewUtils
import java.lang.ref.WeakReference

/**
 * LoadingUtils
 *
 * @author Created by 半仙 on 2023/10/30/030 17:13
 */
object LoadingUtils {

    private var loadingPresenter: WeakReference<LoadingPresenter>? = null
    private var showLoading: Boolean = false

    /**
     * 显示loading
     *
     * @param message 消息
     * @param cancelable 是否可以取消，默认为false
     * @param hasShadowBg 是否有阴影背景，默认为true
     * @param cancelTime 超时取消时间，超过该时间后会自动隐藏，默认为15秒
     * @param waitTime 等待显示时间
     * @param useView 是否使用view显示
     * @param onCancelListener 取消监听
     * @param onDismissListener 消失监听
     */
    fun showLoadingDialog(
        context: Context? = null,
        @StringRes message: Int,
        cancelable: Boolean = false,
        hasShadowBg: Boolean = false,
        cancelTime: Long = 15 * 1000L,
        waitTime: Long = 500L,
        useView: Boolean = true,
        onCancelListener: OnCancelListener? = null,
        onDismissListener: OnDismissListener? = null
    ) {
        showLoadingDialog(
            context,
            StringUtils.getString(message),
            cancelable,
            hasShadowBg,
            cancelTime,
            waitTime,
            useView,
            onCancelListener,
            onDismissListener
        )
    }

    /**
     * 显示loading
     *
     * @param message 消息
     * @param cancelable 是否可以取消，默认为false
     * @param hasShadowBg 是否有阴影背景，默认为false
     * @param cancelTime 超时取消时间，超过该时间后会自动隐藏，默认为15秒
     * @param waitTime 等待显示时间
     * @param useView 是否使用view显示
     * @param onCancelListener 取消监听
     * @param onDismissListener 消失监听
     */
    fun showLoadingDialog(
        context: Context? = null,
        message: String,
        cancelable: Boolean = false,
        hasShadowBg: Boolean = false,
        cancelTime: Long = 15 * 1000L,
        waitTime: Long = 500L,
        useView: Boolean = true,
        onCancelListener: OnCancelListener? = null,
        onDismissListener: OnDismissListener? = null
    ) {
        if (loadingPresenter != null) {
            return
        }

        val finalContext = context ?: ViewUtils.findTopAliveActivity() ?: return
        if (finalContext is LifecycleOwner) {
            finalContext.lifecycle.addObserver(object : LifecycleEventObserver {
                override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                    if (event == Lifecycle.Event.ON_DESTROY) {
                        dismissLoadingDialog()
                    }
                }
            })
        }

        loadingPresenter = WeakReference(
            LoadingPresenter.create(message)
                .setShadowBg(hasShadowBg)
                .setCancelable(cancelable)
                .setCancelTime(cancelTime)
                .setWaitTime(waitTime)
                .setUseView(useView)
                .apply {
                    if (onCancelListener != null) {
                        setOnCancelListener(onCancelListener)
                    }
                }
                .setOnDismissListener {
                    if (!showLoading || loadingPresenter?.get() == it) {
                        loadingPresenter = null
                    }

                    onDismissListener?.onDismiss(it)
                }
                .showLoading(finalContext)
        )

        showLoading = true
    }

    /**
     * 隐藏loading
     *
     * @param waitTime 等待隐藏时间
     */
    fun dismissLoadingDialog(waitTime: Long = 0) {
        showLoading = false
        loadingPresenter?.get()?.hideLoading(waitTime)
        loadingPresenter = null
    }
}